package com.domel76;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.domel76.quickfilm.beans.CinemaBean;
import com.domel76.quickfilm.beans.FilmBean;
import com.domel76.quickfilm.common.Constans;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

public class Film extends ListActivity {
	// ArrayList<String> films = new ArrayList<String>();
	ArrayList<FilmBean> filmsBean = new ArrayList<FilmBean>();
	List<Map<String, String>> data = new ArrayList<Map<String, String>>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		SQLiteDatabase baza = null;

		try {

			baza = this.openOrCreateDatabase(Constans.DB_NAME, MODE_PRIVATE, null);
			Cursor cursor = baza.rawQuery("SELECT id, title,shortDescription,runtime,rating,country,release, genre FROM Film order by title", null);

			if (cursor.moveToFirst()) {
				do {
					Map<String, String> map = new HashMap<String, String>();
					String title = cursor.getString(cursor.getColumnIndex("title"));
					int id = cursor.getInt(cursor.getColumnIndex("id"));
					String shortDescription = cursor.getString(cursor.getColumnIndex("shortDescription"));
					String runtime = cursor.getString(cursor.getColumnIndex("runtime"));
					String rating = cursor.getString(cursor.getColumnIndex("rating"));
					String country = cursor.getString(cursor.getColumnIndex("country"));
					String release = cursor.getString(cursor.getColumnIndex("release"));
					String genre = cursor.getString(cursor.getColumnIndex("genre"));
					filmsBean.add(new FilmBean(id, title));
					map.put("title", title);
					map.put("shortDescription", shortDescription);
					map.put("runtime", runtime);
					map.put("rating", "Ocena: " + rating);
					map.put("country", country);
					map.put("release", release);
					map.put("genre", genre);
					data.add(map);
				} while (cursor.moveToNext());
			}

			// this.setListAdapter(new ArrayAdapter<String>(this,
			// android.R.layout.simple_list_item_1, films));
			ListAdapter a = new SimpleAdapter(this, data, R.layout.film, new String[] { "title", "shortDescription", "runtime", "rating", "country", "release",
					"genre" }, new int[] { R.id.title, R.id.short_description, R.id.runtime, R.id.rating, R.id.country, R.id.release, R.id.genre });
			this.setListAdapter(a);
		} catch (SQLiteException e) {
			Log.e(getClass().getSimpleName(), "Could not create or Open the database");
		} finally {
			if (baza != null)
				baza.close();
		}

		getListView().setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

				Intent intent = new Intent(getApplicationContext(), Seans.class);
				intent.putExtra("filmId", filmsBean.get(arg2).id);
				startActivityForResult(intent, 1338);

			}
		});

		getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

				Intent intent = new Intent(getApplicationContext(), FilmDetails.class);
				intent.putExtra("filmId", filmsBean.get(arg2).id);
				startActivityForResult(intent, 1338);
				return true;

			}
		});
	}
}